package com.greenway.tools;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Map.Entry;

import org.primefaces.json.JSONException;
import org.primefaces.json.JSONObject;
import org.primefaces.model.map.Marker;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.greenway.tools.JsonReader;

public class JsonAddressReader {
	private static String encodeParams(final Map<String, String> params) {
	    final String paramsUrl = Joiner.on('&').join(
	            Iterables.transform(params.entrySet(), new Function<Entry<String, String>, String>() {

	                @Override
	                public String apply(final Entry<String, String> input) {
	                    try {
	                        final StringBuffer buffer = new StringBuffer();
	                        buffer.append(input.getKey());
	                        buffer.append('=');
	                        buffer.append(URLEncoder.encode(input.getValue(), "utf-8"));
	                        return buffer.toString();
	                    } catch (final UnsupportedEncodingException e) {
	                        throw new RuntimeException(e);
	                    }
	                }
	            }));
	    return paramsUrl;
	}
	
	public static String getByAdress(String address){
		double lng = 0.0;
	    double lat = 0.0;
		final String baseUrl = "http://maps.googleapis.com/maps/api/geocode/json";
	    final Map<String, String> params = Maps.newHashMap();
	    params.put("sensor", "false");
	    params.put("address", address);
	    final String url = baseUrl + '?' + encodeParams(params);
	    System.out.println(url);
	    
		try {
			final JSONObject response = JsonReader.read(url);
		    JSONObject location;
			location = response.getJSONArray("results").getJSONObject(0);
			location = location.getJSONObject("geometry");
		    location = location.getJSONObject("location");
		    lng = location.getDouble("lng");
		    lat = location.getDouble("lat");
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	    
	    return lat + "," + lng;
	}
	
	public static String getByCoord(Marker marker){
		
		double lat = marker.getLatlng().getLat();
		double lng = marker.getLatlng().getLng();
		
		String formattedAddress = new String();
		final String baseUrl = "http://maps.googleapis.com/maps/api/geocode/json";
	    final Map<String, String> params = Maps.newHashMap();
	    params.put("language", "ru");
	    params.put("sensor", "false");
	    params.put("latlng", lat + "," + lng);
	    final String url = baseUrl + '?' + encodeParams(params);
	    System.out.println(url);
	    JSONObject response;
		try {
			response = JsonReader.read(url);
			final JSONObject location = response.getJSONArray("results").getJSONObject(0);
		    formattedAddress = location.getString("formatted_address");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
	    return formattedAddress;
	}
}
